package com.github.bluesbruce.h2.web;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.bluesbruce.h2.service.api.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * H2简单的接口
 *
 * @author BBF
 */
@Controller
@RequestMapping("/")
public class H2Controller {

    private static final Logger LOGGER = LoggerFactory.getLogger(H2Controller.class);

    @Resource
    private ObjectMapper objectMapper;

    @Resource
    private UserService userService;

    @ResponseBody
    @GetMapping(value = "/getList.json", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getDataList(@RequestParam(required = false) String name,
                              @RequestParam(required = false) String password) throws JsonProcessingException {
        return objectMapper.writeValueAsString(userService.getList(name, password));
    }

    @ResponseBody
    @GetMapping(value = "/getOne.json", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getDataList(@RequestParam Integer id) throws JsonProcessingException {
        return objectMapper.writeValueAsString(userService.getOne(id));
    }
}
